package com.wei.wiki.controller;

/*
 * @author : ShunWei
 * @date : 2022/1/27 17:20
 * @Description:
 */

import com.wei.wiki.req.EbookQueryReq;
import com.wei.wiki.req.EbookSaveReq;
import com.wei.wiki.resp.CommonResp;
import com.wei.wiki.resp.EbookQueryResp;
import com.wei.wiki.resp.PageResp;
import com.wei.wiki.service.EbookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/ebook")
@Api(tags = "EbookController", description = "电子书控制类")
public class EbookController {
    private EbookService ebookService;

    @Autowired
    public void setEbookService(EbookService ebookService) {
        this.ebookService = ebookService;
    }

    @GetMapping("/list")
    @ApiOperation("电子书信息列表")
    public CommonResp getEbookList(@Valid EbookQueryReq req) {
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        PageResp<EbookQueryResp> bookList = ebookService.bookList(req);
        resp.setSuccess(true);
        resp.setContent(bookList);
        resp.setMessage("success");
        return resp;
    }

    @PostMapping("/save")
    @ApiOperation("新增、修改电子书")
    public CommonResp save(@RequestBody @ApiParam("电子书对象") EbookSaveReq req) {
        CommonResp resp = new CommonResp<>();
        ebookService.save(req);
        resp.setSuccess(true);
        resp.setMessage("success");
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除电子书")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        ebookService.delete(id);
        return resp;
    }
}
